package com.wk.myhandle;

public class MyHandler {

    private MyMessageQueue  mQueue;

    public MyHandler() {
        MyLooper mLooper = MyLooper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                    "Can't create handler inside thread " + Thread.currentThread()
                            + " that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
    }

    public void handleMessage(MyMessage msg) {

    }

    public void dispatchMessage(MyMessage msg){
        handleMessage(msg);
    }

    public void sendMessage(MyMessage msg){
        MyMessageQueue queue = mQueue;
        if (queue != null) {
            enqueueMessage(queue, msg);
        }
    }

    private void enqueueMessage(MyMessageQueue mQueue, MyMessage msg){
        msg.target = this;
        mQueue.enqueueMessage(msg);
    }
}